from Generators import arithemtic_progression
import itertools
import sys
import collections

def ap_triples(a,d):
    pprev = None
    prev = None
    for p in arithemtic_progression(a,d):
        if pprev is None:
            pprev = p
        elif prev is None:
            prev = p
        else:
            yield (p,prev,pprev)
            pprev = prev
            prev = p

def run():
    N = 50*10**6
    dc = collections.defaultdict(int)
    for a in range(1,N):
        print a
        for d in itertools.count(a):
            nset = list(itertools.dropwhile(lambda x: x[0]*x[0] - x[1]*x[1] - x[2]*x[2] > N, itertools.takewhile(lambda x: 0 <= x[0]*x[0] - x[1]*x[1] - x[2]*x[2], ap_triples(a,d))))
            if len(nset) > 0:
                for p in nset:
                    dc[p[0]*p[0] - p[1]*p[1] - p[2]*p[2]] += 1
            else:
                break


    print len(dict((k,v) for k, v in dc.iteritems() if v == 1 and 0 < k < N))

def brute_force(n):
    triples = set()
    for a in range(1,1000):
        for d in range(a, a + 100):
            for x in itertools.islice(ap_triples(a,d),100):
                if x[0]*x[0] - x[1]*x[1] - x[2]*x[2] == n:
                    triples.add(x)
    return triples


run()
